<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
if (!isset($_SESSION)) {
    session_start();
}

class Application_Controllers_UserController {

    private static $session;

    public function __construct($action) {
        self::$session = Factory::buildObjectClass('Start_sesion');
        switch ($action) {
            case 'ValidateUserAction':
                $this->_validateUser();
                break;
            case 'ValidatePasswordAction':
                $this->_validateUserPassword();
                break;
            case 'ShowListAction':
                $this->_showList();
                break;
            case 'ShowIndexAction':
                $this->_showList();
                break;
            case 'ShowRegisterAction':
                $this->_showRegister();
                break;
            case 'ShowModifyPermissionsAction':
                $this->_showModifyPermissions();
                break;

            case 'ShowModifyUserAction':
                $this->_showModifyUserLogin();
                break;

            case 'SearchUserAction':
                $this->_searchUser();
                break;
            case 'SaveAction':
                $this->_saveUser();
                break;
            case 'UpdateAction':
                $this->_updateUser();
                break;
            case 'DeleteAction':
                $this->_deleteUser();
                break;
            case 'ListAction':
                $this->_listUser();
                break;
            case 'LoginAction':
                $this->_loginUser();
                break;
            case 'CloseSessionAction':
                $this->_closeSession();
                break;
//            
        }
    }

    private function _showModifyUserLogin() {
        if (self::$session->validateStartSesion()) {
            if (!self::$session->validateSesion()) {
                $objViewUser = new Application_Views_UserView();
                $objViewUser->showModifyUser();
            } else {
                self::$session->redirect();
            }
        } else {
            self::$session->redirect();
        }
    }

    private function _validateUser() {
        $objModel = new Application_Models_UserModel();
        $objModel->set_user($_POST['userName']);
        echo $objModel->_validateUser();
    }

    private function _validateUserPassword() {
        $objModel = new Application_Models_UserModel();
        $objModel->set_user($_POST['userName']);
        $objModel->set_password($_POST['userPassword']);
        echo $objModel->_validateUserPassword();
    }

    private function _searchUser() {
        $objModel = new Application_Models_UserModel();
        $objModel->set_user($_POST['userName']);
        echo json_encode($objModel->_searchUserValidate());
    }

    private function _showList() {
        if (self::$session->validateStartSesion()) {
            if (!self::$session->validateSesion()) {
                $objView = new Application_Views_UserView();
                $objView->showList();
            } else {
                self::$session->redirect();
            }
        } else {
            self::$session->redirect();
        }
    }

    private function _showRegister() {
        if (self::$session->validateStartSesion()) {
            if (!self::$session->validateSesion()) {
                $objVier = new Application_Views_UserView();
                $objVier->showRegister();
            } else {
                self::$session->redirect();
            }
        } else {
            self::$session->redirect();
        }
//        $objVier = new Application_Views_UserView();
//        $objVier->showRegister();
    }

    private function _showModifyPermissions() {
        $objVier = new Application_Views_UserView();
        $objVier->showModifyPermissions();
    }

    private function _saveUser() {
        $objModel = new Application_Models_UserModel();
        $objModel->set_fkTypeUser($_POST['fkTypeUser']);
        $objModel->set_fkWorkPeople($_POST['fkWorkPeople']);
        $objModel->set_user($_POST["UserName"]);
        $objModel->set_password($_POST['UserPassword']);
        $objModel->_saveUser();
    }

    private function _updateUser() {
        $objModel = new Application_Models_UserModel();
        $objModel->set_pkUser($_POST['pkUser']);
        $objModel->set_fkTypeUser($_POST['fkTypeUser']);

        $objModel->_updateUser();
        echo json_encode("true");
    }

    private function _deleteUser() {
        $objModel = new Application_Models_UserModel();
        $objModel->set_pkUser($_POST['pkUser']);
//        $objModel->set_fkTypeUser($_POST['fkTypeUser']);

        $objModel->_deleteUser();
        echo json_encode("true");
    }

    private function _listUser() {
        $objModelUser = new Application_Models_UserModel();
        $objModelUser->_listUser();
    }

    private function _loginUser() {
        $objModelUser = new Application_Models_UserModel();
        $objModelUser->set_user($_REQUEST['userName']);
        $objModelUser->set_password($_REQUEST['userPassword']);

        $array = $objModelUser->_login();


        for ($i = 0; $i < count($array); $i++) {
            self::$session->startSession($array[$i]['id'], $array[$i]['user'], $array[$i]['pkTypeUser'], $array[$i]['names'], $array[$i]['lastName'], $array[$i]['descriptionTypeUser']);
            ;
        }
    }

    private function _closeSession() {

        self::$session->closeSesion();
        self::$session->redirect();
    }

}
